<script>
function randomStr() {
  return Math.floor(Math.random() * 0xffff).toString(16)
}
const supportList = {
  // 办事处
  office: {
    props: { label: 'name', value: 'code' },
    paramField: 'keyWord',
    requestMethod: 'post',
    focusQuery: true,
    config: {
      title: '办事处',
      interfacePath: '/select-service/api/v1/organization/page',
      filtersRaw:	[
        { property: 'isOffice', operator: '=', value: 1, id: randomStr() }
      ],
      setting: [
        {
          prop: 'code',
          title: '办事处编码'
        },
        {
          prop: 'name',
          title: '办事名称'
        }
      ]
    }
  }
}
export default {
  name: 'CSearchDialog',
  props: {
    type: {
      type: String,
      default: 'unkonwn'
    },
    value: {
      type: [String, Array],
      default: ''
    },
    label: {
      type: [String, Array],
      default: ''
    }
  },
  render(h) {
    const config = supportList[this.type]
    if (!config) {
      return h('span', '配置不正确')
    }
    const valueName = config.props.value
    const labelName = config.props.label
    return h('input-with-dialog', {
      props: { ...config, ...this.$attrs },
      on: {
        ...this.$listeners,
        input: v => {
          if (this.$attrs && 'multiple' in this.$attrs) {
            this.$emit('input', v.map(e => e[valueName]))
            this.$emit('update:label', v.map(e => e[labelName]))
          } else if (v[0]) {
            this.$emit('input', v[0][valueName])
            this.$emit('update:label', v[0][labelName])
          }
        }
      }
    })
  }
}
</script>
